package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    /**
     * 保存地理位置
     * @param longitude 经度
     * @param latitude 纬度
     * @param addrStr 地址描述
     * @param userId 登陆用户id
     */
    public void save(Double longitude, Double latitude, String addrStr, Long userId) {
        //1.根据用户id查询userLocation对象
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);

        //2.判断userLocation是否存在
        if (userLocation == null) {
            //3.若不存在,就是新增操作
            userLocation = new UserLocation();

            userLocation.setUserId(userId);
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));//经度在前 纬度在后
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(userLocation.getCreated());

            mongoTemplate.save(userLocation);

        }else{
            //4.若存在,就是更新操作
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));//经度在前 纬度在后
            userLocation.setAddress(addrStr);

            //设置上次更新时间
            userLocation.setLastUpdated(userLocation.getUpdated());
            userLocation.setUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);
        }
    }

    @Override
    //搜附近
    public List<Long> searchNear(Long userId, Long distance) {
        //1.查询userLocation获取我的坐标
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);

        //2.注意非空判断
        if (userLocation != null) {
            //获取我的坐标(圆心)
            GeoJsonPoint point = userLocation.getLocation();
            //半径
            Distance radius = new Distance(distance/1000, Metrics.KILOMETERS);

            Circle circle = new Circle(point,radius);

            //3.创建条件,以”我的坐标”为中心,以”距离”为半径,搜索在圈里面的用户地址集合
            Query query = new Query(Criteria.where("location").withinSphere(circle));

            List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);

            //4.遍历集合,获取这些用户的id放入一个新集合中
            if (CollUtil.isNotEmpty(userLocations)) {
                List<Long> ids = userLocations.stream().map(UserLocation::getUserId).collect(Collectors.toList());
                return ids;
            }
        }

        //5.返回新集合
        return null;
    }
}
